package controller

import (
	"fmt"
	"gopkg.in/olivere/elastic.v5"
	"goruntimeCrawler/frontend/model"
	"goruntimeCrawler/frontend/view"
	"net/http"
	"strconv"
	"strings"
)

type SearchResultHandler struct {
   view view.SearchResultView
   client *elastic.Client
}

func (s SearchResultHandler) ServeHTTP(
     w http.ResponseWriter,req *http.Request) {
    q:=strings.TrimSpace(req.FormValue("q"))
    from,err:=strconv.Atoi(
    	req.FormValue("from"),
    	)
	if err!=nil {
		from=0
	}
    fmt.Println(q,from)
    s.view.Render(w,model.SearchResult{})
}

func CreateSeachResultHandler(
	template string) SearchResultHandler {
		client,err:=elastic.NewClient(
			elastic.SetSniff(false))
		if err!=nil{

		}
	return SearchResultHandler{
		view:view.CreateSearchResultView(template),
		client:client,
	}
}
